import time
import datetime
import sys
import os
#sys.path.append('C:/Project/PythonSpider')
#sys.path.append('D:/Project/PythonSpider')


def Func():
    os.system("scrapy runspider " + "Spider_EMNews.py")

filePath = os.getcwd()
runningTime = datetime.time(0, 0)
originTime = datetime.time(0, 0)
periodMinute = 1
periodHour = 0
periodSecond = 0

print("Spider Schedule Started")
while True:
    #print(filePath)
    now = datetime.datetime.now()
    today = now.date()
    today = datetime.datetime.combine(today, originTime)

    #
    period = periodHour * 3600 + periodMinute * 60 + periodSecond

    #
    passedSeconds = (now - today).total_seconds()
    n = int(passedSeconds / period)
    nextSeconds = (n + 1) * period
    sleepSecond = nextSeconds - passedSeconds

    print("Sleep Time", sleepSecond)
    time.sleep(sleepSecond)


    print("Run", datetime.datetime.now())
    Func()

    # time.sleep(10)